package cn.lili.modules.grab.alibaba.serviceimpl;

import cn.lili.modules.grab.alibaba.mapper.GoodsGrabDetailMapper;
import cn.lili.modules.grab.alibaba.service.GoodsGrabDetailService;
import cn.lili.modules.grab.entity.dos.GoodsGrab;
import cn.lili.modules.grab.entity.dos.GoodsGrabDetail;
import cn.lili.modules.grab.alibaba.service.GoodsGrabService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class GoodsGrabDetailServiceImpl extends ServiceImpl<GoodsGrabDetailMapper, GoodsGrabDetail> implements GoodsGrabDetailService {

    @Autowired
    private GoodsGrabService goodsGrabService;

    @Transactional
    @Override
    public String addGoodsGrabDetail(GoodsGrabDetail data) {
        try{
            this.save(data);
            GoodsGrab goodsGrab = goodsGrabService.getById(data.getGrabId());
            if(null!=goodsGrab){
                int $total_num = goodsGrab.getTotalNum() + 1;
                int $success_num = goodsGrab.getSuccessNum();
                int $error_num = goodsGrab.getErrorNum();
                if(data.getStatus()==1){
                    $success_num+=1;

                }else{
                    $error_num+=1;
                }
                goodsGrab.setTotalNum($total_num);
                goodsGrab.setSuccessNum($success_num);
                goodsGrab.setErrorNum($error_num);
                goodsGrabService.updateById(goodsGrab);
                return null;
            }
            return "数据不存在";
        }catch (Exception e){
            return "系统出错了";
        }
    }
}
